package com.zhen.aikan.controller;

import com.zhen.aikan.entity.Danmu;
import com.zhen.aikan.service.IDanmuService;
import com.zhen.aikan.vo.DanmuResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lijiazhen
 * @since 2023-09-11
 */
@RestController
@RequestMapping("/api/danmu")
public class DanmuController {
    @Autowired
    private IDanmuService danmuService;

    @ApiOperation("获取对应视频对应集的弹幕")
    @GetMapping("/v3")
    public DanmuResult getDanmu(@RequestParam(name = "id") String id) {
        //id:视频id_指定视频的集
        String[] s = id.split("_");
        List<List<Object>> danmus = danmuService.getDanmuByVodIdAndEpisode(s[0], s[1]);
        return DanmuResult.ok(danmus);
    }

    @ApiOperation("保存弹幕")
    @PostMapping("/v3")
    public DanmuResult saveDanmu(@RequestBody Map<String, String> param) {
        String token = param.get("token");
        if (!StringUtils.hasText(token)) {
            return DanmuResult.fail("发送弹幕需要登录");
        }
        boolean ok = danmuService.saveDanmu(param);
        if (!ok) {
            return DanmuResult.fail("发送弹幕失败");
        }
        return DanmuResult.ok(null);
    }
}
